package com.longdaji.decoration.ui.activitiesOfMine.allOrder;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.longdaji.decoration.Constants;
import com.longdaji.decoration.R;
import com.longdaji.decoration.adapter.AllOrderAdapter;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Orders;
import com.longdaji.decoration.ui.activitiesOfMine.pendingEvaluated.EvaluateActivity;
import com.longdaji.decoration.ui.activitiesOfMine.pendingEvaluated.OrderDetailActivity;
import com.longdaji.decoration.ui.activitiesOfMine.pendingPayment.payment.PaymentActivity;
import com.longdaji.decoration.ui.activitiesOfMine.pendingReceiving.SeeLogisticsActivity;
import com.longdaji.decoration.ui.logistics.LogisticsActivity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AllOrderActivity extends BaseActivity implements AllOrderContract.IView {

    @Inject
    AllOrderContract.IPresenter mPresenter;

    @BindView(R.id.tv_tool_title)
    TextView tvTitle;
    @BindView(R.id.rv_all_order)
    RecyclerView rvAllOrder;
    @BindView(R.id.sr_all_order)
    SwipeRefreshLayout srAllOrder;
    private AllOrderAdapter mAdapter;
    private List<Orders.OrdersBean.OrderBean> mOrderList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_order);
        setUnBinder(ButterKnife.bind(this));

        if (mPresenter != null)
            mPresenter.setView(this);
        tvTitle.setText("我的订单");

        initView();
    }

    private void initView() {
        rvAllOrder.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new AllOrderAdapter(mOrderList);
        initItemAction();
        rvAllOrder.setAdapter(mAdapter);
        srAllOrder.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        srAllOrder.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData();
            }
        });
        mPresenter.initData();
    }

    private void refreshData() {
        mPresenter.initData();
    }

    private void initItemAction() {
        mAdapter.setOnAdapterItemClickListener(new AllOrderAdapter.AdapterItemClickListener() {
            @Override
            public void onPayClick(int position) {
                Intent intent = new Intent(AllOrderActivity.this, PaymentActivity.class);
                intent.putExtra(Constants.ORDER_NO, mOrderList.get(position).getOrderNo());
                startActivity(intent);
            }

            @Override
            public void onCancelOrderPaymentClick(int position) {
                mPresenter.onCancelOrderPaymentClick(mOrderList.get(position).getOrderNo());

            }

            @Override
            public void onCancelOrderDeliveryClick(int position) {
                mPresenter.onCancelOrderDeliveryClick(mOrderList.get(position).getOrderNo());
            }

            @Override
            public void onSeeLogisticsReceivingClick(int position) {
                Intent intent = new Intent(AllOrderActivity.this, SeeLogisticsActivity.class);
                intent.putExtra(Constants.ORDER_NO, mOrderList.get(position).getOrderNo());
                startActivity(intent);
            }

            @Override
            public void onConfirmReceivingClick(int position) {
                mPresenter.onConfirmOrderClick(mOrderList.get(position).getOrderNo());
            }

            @Override
            public void onRefundGoodsClick(int position) {
                Intent intent = new Intent(AllOrderActivity.this, OrderDetailActivity.class);
                intent.putExtra(Constants.ORDER_NO, mOrderList.get(position).getOrderNo());
                startActivity(intent);
            }

            @Override
            public void onSeeLogisticsEvaluateClick(int position) {
                Intent intent = new Intent(AllOrderActivity.this, SeeLogisticsActivity.class);
                intent.putExtra(Constants.ORDER_NO, mOrderList.get(position).getOrderNo());
                startActivity(intent);
            }

            @Override
            public void onEvaluateClick(int position) {
                Intent intent = new Intent(AllOrderActivity.this, OrderDetailActivity.class);
                intent.putExtra(Constants.ORDER_NO, mOrderList.get(position).getOrderNo());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }


    @OnClick({R.id.rl_tool_back})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_tool_back:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void showGetOrderSucceed(List<Orders.OrdersBean.OrderBean> allOrderList) {
        Collections.reverse(allOrderList);
        if (allOrderList.size() > 0) {
            mAdapter.setData(allOrderList);
        } else {
            Toast.makeText(this, "无任何订单", Toast.LENGTH_SHORT).show();
            mAdapter.clearData();
        }
        if (srAllOrder != null && srAllOrder.isRefreshing()) {
            srAllOrder.setRefreshing(false);
        }
    }

    @Override
    public void showCancelOrderSucceed() {
        Toast.makeText(this, "成功取消订单", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showRefundSucceed() {
        Toast.makeText(this, "退款申请成功", Toast.LENGTH_SHORT).show();
    }
}
